<template>
	<el-container>
		<el-aside width="200px" v-loading="showGrouploading">
			<el-container>
				<el-header>
					<el-input placeholder="输入关键字进行过滤" v-model="groupFilterText" clearable></el-input>
				</el-header>
				<el-main class="nopadding">
					<el-tree ref="group" class="menu" node-key="deptId" :data="group" :props="groupsProps" :current-node-key="''" :highlight-current="true" :expand-on-click-node="false" :filter-node-method="groupFilterNode" @node-click="groupClick"></el-tree>
				</el-main>
			</el-container>
		</el-aside>
		<el-container>
			<el-header>
				<div class="left-panel">
					<el-button type="success" icon="el-icon-download" @click="getExpUserRecord"></el-button>
					<el-button type="primary" icon="el-icon-plus" @click="add"></el-button>
					<el-button type="danger" plain icon="el-icon-delete" :disabled="selection.length==0" @click="batch_del"></el-button>
				</div>
				<div class="right-panel">
					<div class="right-panel-search">
						<el-input v-model="search.keyword" placeholder="姓名 / 工号 / 身份证号" clearable></el-input>
						<el-button type="primary" icon="el-icon-search" @click="upsearch"></el-button>
					</div>
				</div>
			</el-header>
			<el-main class="nopadding">
				<scTable ref="table" :apiObj="apiObj" @selection-change="selectionChange" stripe remoteSort remoteFilter>
					<el-table-column type="selection" width="50"></el-table-column>
					<el-table-column label="#" type="index" width="50"></el-table-column>
					<el-table-column label="头像" width="80">
						<template #default="scope">
							<el-avatar :src="scope.row.avatar" size="small"></el-avatar>
						</template>
					</el-table-column>
					<el-table-column label="姓名" prop="userName" width="100"></el-table-column>
					<el-table-column label="性别" prop="sex" width="60">
						<template #default="scope">
							<el-button type="primary" size="small" v-if="scope.row.sex==='1'">男</el-button>
							<el-button type="danger" size="small" v-else-if="scope.row.sex==='0'">女</el-button>
							<el-button type="info" size="small" v-else>保密</el-button>
						</template>
					</el-table-column>
					<el-table-column label="所在部门" prop="deptName" width="100"></el-table-column>
					<el-table-column label="入职时间" prop="employedTime" width="100"></el-table-column>
					<el-table-column label="在职状态" prop="workStatusName" width="100"></el-table-column>
					<el-table-column label="工种" prop="workTypeName" width="100"></el-table-column>
					<el-table-column label="工号" prop="workNo" width="150"></el-table-column>
					<el-table-column label="联系电话" prop="phone" width="150"></el-table-column>
					<el-table-column label="操作" fixed="right" align="right" width="170">
						<template #default="scope">
							<el-button-group>
								<el-button text type="primary" size="small" @click="table_show(scope.row, scope.$index)">查看</el-button>
								<el-button text type="primary" size="small" @click="table_edit(scope.row, scope.$index)">编辑</el-button>
								<el-popconfirm title="确定删除吗？" @confirm="table_del(scope.row, scope.$index)">
									<template #reference>
										<el-button text type="primary" size="small">删除</el-button>
									</template>
								</el-popconfirm>
							</el-button-group>
						</template>
					</el-table-column>
				</scTable>
			</el-main>
		</el-container>
	</el-container>
	<save-dialog v-if="dialog.save" ref="saveDialog" @success="handleSuccess" @closed="dialog.save=false"></save-dialog>
</template>

<script>
import saveDialog from './save'

export default {
	name: 'Hr User',
	components: {
		saveDialog,
	},
	data() {
		return {
			dialog: {
				save: false
			},
			showGrouploading: false,
			groupFilterText: '',
			group: [],
			groupsProps: {
				label:"deptName",
				value: "deptId"
			},
			apiObj: this.$API.hr.hrUser.getHrUserListByDeptId,
			selection: [],
			search: {
				keyword: null
			}
		}
	},
	watch: {
		groupFilterText(val) {
			this.$refs.group.filter(val);
		}
	},
	mounted() {
		this.getDeptGroup()
	},
	methods: {
		getExpUserRecord(){
			this.$API.hr.hrUser.getExpUserRecord.fileDownload();
		},
		//添加
		add(){
			this.dialog.save = true
			this.$nextTick(() => {
				this.$refs.saveDialog.open()
			})
		},
		//编辑
		table_edit(row){
			this.dialog.save = true
			this.$nextTick(() => {
				this.$refs.saveDialog.open('edit').setData(row)
			})
		},
		//查看
		table_show(row){
			this.dialog.save = true
			this.$nextTick(() => {
				this.$refs.saveDialog.open('show').setData(row)
			})
		},
		//删除
		async table_del(row, index){
			const loading = this.$loading();
			var reqData = {userId: row.userId}
			var res = await this.$API.hr.hrUser.deleteHrUser.post(reqData);
			if(res.code == 200){
				this.$refs.table.refresh();
				this.$message.success(res.message);
			}else{
				this.$alert(res.message, "提示", {type: 'error'})
			}
			loading.close();
		},
		//批量删除
		async batch_del(){
			this.$confirm(`确定删除选中的 ${this.selection.length} 项吗？`, '提示', {
				type: 'warning'
			}).then(async() => {
				const loading = this.$loading();
				var userIdArr=[];
				this.selection.forEach(item => {
					userIdArr.push(item.userId);
				})
				var res = await this.$API.hr.hrUser.deleteByHrUserIds.post({accountIds: userIdArr});
				if (res.code == 200) {
					this.$refs.table.refresh()
					this.$message.success(res.message)
				} else {
					this.$alert(res.message, "提示", {type: 'error'})
				}
				loading.close();
			}).catch(() => {

			})
		},
		//表格选择后回调事件
		selectionChange(selection){
			this.selection = selection;
		},
		//加载树数据
		async getDeptGroup(){
			this.showGrouploading = true;
			var res = await this.$API.hr.hrDepartment.getHrDepartmentTree.get();
			this.showGrouploading = false;
			var allNode ={deptId: '', deptName: '所有'}
			res.data.unshift(allNode);
			this.group = res.data;
		},
		//树过滤
		groupFilterNode(value, data){
			if (!value) return true;
			return data.deptName.indexOf(value) !== -1;
		},
		//树点击事件
		groupClick(data){
			var params = {
				deptId: data.deptId
			}
			this.$refs.table.reload(params)
		},
		//搜索
		upsearch(){
			this.$refs.table.upData(this.search)
		},
		//本地更新数据
		handleSuccess(data, mode){
			this.$refs.table.refresh()
		}
	}
}
</script>

<style>
</style>
